#!/usr/bin/env python
"""\
convert system time to gnome desktop background color
the upper desktop area reflects minutes while the hours 
are responsible for the bottom part of the desktop
"""

import ImageColor
import datetime, time
import gconf
import sys

bg_path = "/desktop/gnome/background/"
color_pattern = "hsl(%i, 75%%, 15%%)"
verbose = False
testing = False
if "testing" in sys.argv:
    testing = True

client = gconf.client_get_default()
#client.set_string(bg_path+"color_shading_type", "horizontal-gradient")
client.set_string(bg_path+"color_shading_type", "vertical-gradient")

def update_colors(tt):
    # set seconds
    sec = float(tt.second + 60 * tt.minute) 
    hue = int(sec * 360 / (60*60))
    rgb = "#%02x%02x%02x"%(ImageColor.getrgb(color_pattern%hue))
    client.set_string(bg_path+"primary_color", rgb)
    if verbose: print "primary", hue,

    # set hour
    #hour = float(tt.hour)
    hour = float(tt.hour + tt.minute/60.0 + tt.second/(60.0*60.0))
    hue  = int(hour * 360 / (24))
    rgb = "#%02x%02x%02x"%(ImageColor.getrgb(color_pattern%hue))
    client.set_string(bg_path+"secondary_color", rgb)
    if verbose: print "secondary", hue,

    print tt, \
	"m=%.2f"%(sec/60.0), \
	"primary=%s"%client.get_string(bg_path+"primary_color"), \
	"h=%.2f"%hour, \
	"secondary=%s"%client.get_string(bg_path+"secondary_color")

try:
    if testing:
	for tt in [datetime.time(h,m,s) 
	    for h in range(0,24,24) 
	    for m in range(0,60,5) 
	    for s in range(0,60,60)
	]:
	    update_colors(tt)
	    time.sleep(1)
    else:
	while True:
	    tt = datetime.datetime.now()
	    update_colors(tt)
	    time.sleep(15)
except KeyboardInterrupt:
    pass

client.set_string(bg_path+"color_shading_type", "solid")
client.set_string(bg_path+"primary_color", "#000000")

